﻿using System;
using System.Collections.Generic;
using System.Text;
using DAL.DateSet.TypeTableAdapters;
using System.Data;
using DAL;
using DAL.DateSet;
namespace BLL.Wrapper
{
    public class TypeWrapper : TypeTableAdapter
    {
        public DAL.DateSet.Type.TypeDataTable getall()
        {
            return GetData();
        }

        public void addARecord(DAL.DateSet.Type.TypeRow row)
        {
            Insert(row.parentID, row.typeName, row.typeFlag, row.typeLevel, row.typeCreator, row.typeCreateDate,row.englishName);
        }

        public void deleteByID(int id)
        {
            string sql = "delete from ModifyRecord where typeID = " + id.ToString();
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            helper.initOle();
            helper.delete(sql);
        }

        public void updateARecord(DAL.DateSet.Type.TypeRow row)
        {
            DAL.DateSet.Type.TypeDataTable table = GetData();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (Convert.ToInt32(table.Rows[i]["typeID"]) == row.typeID)
                {
                    table.Rows[i]["parentID"] = row.parentID;
                    table.Rows[i]["typeName"] = row.typeName;
                    table.Rows[i]["typeFlag"] = row.typeFlag;
                    table.Rows[i]["typeLevel"] = row.typeLevel;

                    table.Rows[i]["typeCreator"] = row.typeCreator;
                    table.Rows[i]["typeCreateDate"] = row.typeCreateDate;
                    break;
                }
            }
            Update(table);
        }

        public DataTable getDataBylevel(int level)
        {
            string sql = "select * from Type where typelevel = " + level.ToString();
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            helper.initOle();
            return  helper.getResultBySql(sql);
        }

        public List<string> getTypeNamesBylevel(int level)
        {
            string sql = "select typeName from Type where typelevel = " + level.ToString();
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            helper.initOle();
            List<string> list = new List<string>();
            DataTable table = new DataTable();
            table = helper.getResultBySql(sql);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(table.Rows[i][0].ToString());
            }
            return list; 
        }

        public List<string> getSubNamebytypeName(string name)
        {
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            string sql = "select typeID from Type where typeName = '" + name + "'";
            DataTable table1 = new DataTable();
            table1 = helper.getResultBySql(sql);
            int id = Convert.ToInt32(table1.Rows[0][0].ToString());
            sql = "select typeID,typeName from Type where parentID = " + id.ToString();
            DataTable table = new DataTable();
            table = helper.getResultBySql(sql);
            List<string> list = new List<string>();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(table.Rows[i][1].ToString());
            }
            return list; 
        }

        public string getParentName(string name)
        {
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            string sql = "select typeID,parentID from Type where typeName = '" + name + "'";
            DataTable table = new DataTable();
            table = helper.getResultBySql(sql);
            int id , parentid = 1;
            if (table.Rows.Count > 0)
            {
                id = Convert.ToInt32(table.Rows[0][0].ToString());
                parentid = Convert.ToInt32(table.Rows[0][1].ToString());
            }
            sql = "select typeName from Type where typeID = " + parentid.ToString();
            table.Clear();
            table = helper.getResultBySql(sql);
            if (table.Rows.Count > 0)
            {
                return table.Rows[0][0].ToString();
            }
            return "";
        }

        public List<string> getSubName(string name)
        {
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            string sql = "select typeID from Type where typeName = '" + name + "'";
            DataTable table = new DataTable();
            List<string> list = new List<string>();
            table = helper.getResultBySql(sql);
            int id = -100;
            if (table.Rows.Count > 0)
            {
                id = Convert.ToInt32(table.Rows[0][0].ToString());
            }
            sql = "select typeName from Type where parentID = " + id.ToString();
            table.Clear();
            table = helper.getResultBySql(sql);

            for (int i = 0; i < table.Rows.Count; i++)
            {
                list.Add(table.Rows[i][0].ToString()); 
            }
            return list;
        }

        public int getLevelByName(string name)
        {
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            string sql = "select typeLevel from Type where typeName = '" + name + "'";
            DataTable table = new DataTable();
            table = helper.getResultBySql(sql);
            if (table.Rows.Count > 0)
            {
                return Convert.ToInt32(table.Rows[0][0].ToString());
            }
            return 0;
        }

        public int getIDByName(string name)
        {
            HouseDiyHelper helper = HouseDiyHelper.GetInstance();
            string sql = "select typeID from Type where typeName = '" + name + "'";
            DataTable table = new DataTable();
            table = helper.getResultBySql(sql);
            return Convert.ToInt32(table.Rows[0][0].ToString());
        }

    }
}
